How to fix fatal error: uncaught error: call to undefined function mysql

您所在的位置:网站首页 result error message How to fix fatal error: uncaught error: call to undefined function mysql

How to fix fatal error: uncaught error: call to undefined function mysql

2023-03-31 12:39| 来源: 网络整理| 查看: 265

The "Fatal error: Uncaught Error: Call to undefined function mysql_connect()" is a common issue encountered by PHP developers. This error message appears when the MySQL extension is not enabled in PHP and the code is trying to call the "mysql_connect()" function, which is part of the deprecated MySQL extension. This error can be fixed by enabling the MySQL extension in PHP, or by using an alternative extension, such as the MySQLi or PDO extensions.

Method 1: Enabling the MySQL Extension

To enable the MySQL extension in PHP, you can follow these steps:

Locate your PHP configuration file (php.ini). You can find this file in your PHP installation directory or by running the phpinfo() function in a PHP file and looking for the "Loaded Configuration File" value.

Open the php.ini file in a text editor.

Search for the following line: ;extension=php_mysql.dll

Remove the semicolon (;) at the beginning of the line to uncomment it.

Save the file and restart your web server.

After enabling the MySQL extension, you can use the mysql_connect() function to establish a connection to your MySQL database. Here's an example:

In this example, we first define the database configuration variables. Then, we use the mysql_connect() function to establish a connection to the database. If the connection fails, we display an error message using the mysql_error() function.

Next, we select the database using the mysql_select_db() function and perform a query using the mysql_query() function. If the query fails, we display an error message using the mysql_error() function.

Finally, we display the results using the mysql_fetch_assoc() function and close the connection using the mysql_close() function.

Note that the mysql_* functions are deprecated as of PHP 5.5.0 and removed as of PHP 7.0.0. It is recommended to use the mysqli or PDO extensions instead.

Method 2: Using the MySQLi Extension

To fix the error "Fatal error: Uncaught Error: Call to undefined function mysql_connect()" in PHP, you can use the MySQLi extension. Here are the steps to do it:

First, you need to make sure that the MySQLi extension is enabled in your PHP installation. You can do this by checking the "php.ini" file or by running the "phpinfo()" function.

Once you have confirmed that the MySQLi extension is enabled, you can use the "mysqli_connect()" function to connect to your MySQL database. Here is an example:

$servername = "localhost"; $username = "username"; $password = "password"; $dbname = "myDB"; // Create connection $conn = mysqli_connect($servername, $username, $password, $dbname); // Check connection if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } echo "Connected successfully";

In this example, we are connecting to a MySQL database with the server name "localhost", username "username", password "password", and database name "myDB". If the connection is successful, the message "Connected successfully" will be displayed.

Once you have established a connection to the database, you can use the various MySQLi functions to perform database operations. For example, to query the database, you can use the "mysqli_query()" function. Here is an example: $sql = "SELECT * FROM myTable"; $result = mysqli_query($conn, $sql); if (mysqli_num_rows($result) > 0) { // output data of each row while($row = mysqli_fetch_assoc($result)) { echo "id: " . $row["id"]. " - Name: " . $row["name"]. " - Email: " . $row["email"]. ""; } } else { echo "0 results"; }

In this example, we are selecting all the rows from a table called "myTable". The "mysqli_query()" function is used to execute the SQL query, and the "mysqli_fetch_assoc()" function is used to fetch the results of the query.

Finally, it is important to close the database connection when you are done with it. You can use the "mysqli_close()" function to do this. Here is an example: mysqli_close($conn);

This will close the database connection that was established earlier.

That's it! By following these steps, you should be able to fix the "Fatal error: Uncaught Error: Call to undefined function mysql_connect()" error in PHP using the MySQLi extension.

Method 3: Using the PDO Extension

To fix the "Fatal error: Uncaught Error: Call to undefined function mysql_connect()" error in PHP, you can use the PDO extension. Follow the steps below:

Create a new PDO object and connect to the MySQL database: $dsn = 'mysql:host=localhost;dbname=your_database_name'; $user = 'your_username'; $password = 'your_password'; try { $pdo = new PDO($dsn, $user, $password); } catch (PDOException $e) { echo 'Connection failed: ' . $e->getMessage(); exit; } Prepare a SQL statement using the PDO prepare method: $sql = 'SELECT * FROM your_table_name WHERE id = :id'; $stmt = $pdo->prepare($sql); Bind the parameter values to the prepared statement using the PDO bindParam method: $id = 1; $stmt->bindParam(':id', $id, PDO::PARAM_INT); Execute the prepared statement using the PDO execute method: $stmt->execute(); Fetch the result set using the PDO fetch method: $result = $stmt->fetch(PDO::FETCH_ASSOC); Close the PDO connection: $pdo = null;

That's it! By using the PDO extension, you can avoid the "Call to undefined function mysql_connect()" error and perform database operations in a secure and efficient way.



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3